package com.hand.wfive.loginAndVip.ui

import android.graphics.Color
import android.text.TextUtils
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import com.hand.wfive.R
import com.hand.wfive.base.BaseActivity
import com.hand.wfive.loginAndVip.ApiConfig
import com.hand.wfive.loginAndVip.LoginConfig
import com.hand.wfive.loginAndVip.MD5Util
import com.hand.wfive.loginAndVip.UserManager
import com.hand.wfive.loginAndVip.model.ApiModel
import com.hand.wfive.util.click.NoFastClickUtils
import com.hand.wfive.util.click.singleClick
import com.rxjava.rxlife.RxLife
import kotlinx.android.synthetic.main.login_activity_change_password.*
import org.jetbrains.anko.toast
import rxhttp.wrapper.param.RxHttp

class ChangePasswordActivity : BaseActivity() {

    override fun getContentViewId() = R.layout.login_activity_change_password

    override fun init() {
        topBar.addLeftImageButton(R.mipmap.login_back, R.id.top_bar_left_image)
            .singleClick { finish() }
        topBar.updateBottomSeparatorColor(Color.TRANSPARENT)
    }

    fun changePasswordBtnClick(v: View) {
        if (NoFastClickUtils.isFastClick()) return
        when (v) {
            login_password_op -> /* 密码显示或隐藏 */ {
                login_password_op.isSelected = !login_password_op.isSelected
                if (login_password_op.isSelected) {
                    login_password_op.setImageResource(R.mipmap.login_password_show)
                    login_password.transformationMethod =
                        HideReturnsTransformationMethod.getInstance()
                } else {
                    login_password_op.setImageResource(R.mipmap.login_password_hide)
                    login_password.transformationMethod =
                        PasswordTransformationMethod.getInstance()
                }
                login_password.setSelection(login_password.length())
            }
            login_password_op1 -> /* 密码1显示或隐藏 */ {
                login_password_op1.isSelected = !login_password_op1.isSelected
                if (login_password_op1.isSelected) {
                    login_password_op1.setImageResource(R.mipmap.login_password_show)
                    login_password1.transformationMethod =
                        HideReturnsTransformationMethod.getInstance()
                } else {
                    login_password_op1.setImageResource(R.mipmap.login_password_hide)
                    login_password1.transformationMethod =
                        PasswordTransformationMethod.getInstance()
                }
                login_password1.setSelection(login_password1.length())
            }
            login_password_op2 -> /* 密码2显示或隐藏 */ {
                login_password_op2.isSelected = !login_password_op2.isSelected
                if (login_password_op2.isSelected) {
                    login_password_op2.setImageResource(R.mipmap.login_password_show)
                    login_password2.transformationMethod =
                        HideReturnsTransformationMethod.getInstance()
                } else {
                    login_password_op2.setImageResource(R.mipmap.login_password_hide)
                    login_password2.transformationMethod =
                        PasswordTransformationMethod.getInstance()
                }
                login_password2.setSelection(login_password2.length())
            }
            change -> /* 修改密码 */ check()
        }
    }

    private fun check() {
        val pass = login_password.text.toString()
        if (pass.isEmpty()) {
            showNormalTip(topBar, "请输入当前密码")
            return
        }
        val pass1 = login_password1.text.toString()
        if (pass1.isEmpty()) {
            showNormalTip(topBar, "请输入新密码")
            return
        }
        if (pass1.length < 6) {
            showNormalTip(topBar, "新密码的长度不能少于6个字符")
            return
        }
        val pass2 = login_password1.text.toString()
        if (pass2 != pass1) {
            showNormalTip(topBar, "密码不一致")
            return
        }

        update(pass, pass1)
    }

    private fun update(pass: String, pass1: String) {
        showLoading("请稍后...")
        val newPassMd5 = MD5Util.getPasswordMd5(pass1)
        RxHttp.postForm(ApiConfig.modifyPw)
            .add("appid", LoginConfig.AppId)
            .add("username", UserManager.getInstance().curUser.username)
            .add("oldPsw", MD5Util.getPasswordMd5(pass))
            .add("newPsw", newPassMd5)
            .asClass(ApiModel::class.java)
            .to(RxLife.toMain(this))
            .subscribe({ apiModel ->
                hideLoading()
                if (apiModel.code == ApiConfig.SUCCESSCODE) {
                    toast("密码修改成功")
                    val user = apiModel.obj
                    user.password = newPassMd5
                    UserManager.getInstance().saveUser(user)
                    finish()
                } else {
                    if (TextUtils.isEmpty(apiModel.msg)) {
                        showNormalTip(topBar, "网络异常，请重试！")
                    } else {
                        showNormalTip(topBar, apiModel.msg)
                    }
                }
            }, {
                hideLoading()
                showNormalTip(topBar, "密码修改失败")
            })
    }
}
